package com.ruoyi.demo.controller.app;

import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.demo.domain.AuctionSessionGoods;
import com.ruoyi.demo.domain.vo.AuctionSessionGoodsVO;
import com.ruoyi.demo.service.IAuctionSessionGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 专场商品关联Controller
 *
 * @author metaee
 * @date 2025-03-06
 */
@Api(tags = "小程序-专场商品")
@RestController
@RequestMapping("/app/sessionGoods")
public class AppAuctionSessionGoodsController extends BaseController
{
    @Autowired
    private IAuctionSessionGoodsService auctionSessionGoodsService;

    /**
     * 查询专场商品列表
     */
    @ApiOperation("查询专场商品列表")
    @GetMapping("/list")
    public TableDataInfo list(AuctionSessionGoods auctionSessionGoods)
    {
        startPage();
        LambdaQueryWrapper<AuctionSessionGoods> wrapper = new LambdaQueryWrapper<>();
        // 根据需要添加查询条件
        if (auctionSessionGoods.getSessionId() != null) {
            wrapper.eq(AuctionSessionGoods::getSessionId, auctionSessionGoods.getSessionId());
        }
        if (auctionSessionGoods.getGoodsId() != null) {
            wrapper.eq(AuctionSessionGoods::getGoodsId, auctionSessionGoods.getGoodsId());
        }
        if (auctionSessionGoods.getStatus() != null) {
            wrapper.eq(AuctionSessionGoods::getStatus, auctionSessionGoods.getStatus());
        }
        wrapper.orderByAsc(AuctionSessionGoods::getDisplayOrder);
        List<AuctionSessionGoods> list = auctionSessionGoodsService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 查询指定专场下的商品列表
     */
    @ApiOperation("查询指定专场下的商品列表")
    @GetMapping("/list/{sessionId}")
    public TableDataInfo listBySessionId(@PathVariable("sessionId") Long sessionId)
    {
        startPage();
        List<AuctionSessionGoodsVO> list = auctionSessionGoodsService.listSessionGoodsWithDetails(sessionId);
        return getDataTable(list);
    }

    /**
     * 获取专场商品关联详细信息
     */
    @ApiOperation("获取专场商品关联详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(auctionSessionGoodsService.getById(id));
    }

    /**
     * 新增专场商品关联
     */
    @ApiOperation("新增专场商品关联")
    @Log(title = "专场商品关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AuctionSessionGoods auctionSessionGoods)
    {
        return toAjax(auctionSessionGoodsService.save(auctionSessionGoods));
    }

    /**
     * 批量添加商品到专场
     */
    @ApiOperation("批量添加商品到专场")
    @Log(title = "专场商品关联", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult addBatch(@RequestBody List<AuctionSessionGoods> goodsList)
    {
        return toAjax(auctionSessionGoodsService.saveBatch(goodsList));
    }

    /**
     * 修改专场商品关联
     */
    @ApiOperation("修改专场商品关联")
    @Log(title = "专场商品关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AuctionSessionGoods auctionSessionGoods)
    {
        return toAjax(auctionSessionGoodsService.updateById(auctionSessionGoods));
    }

    /**
     * 删除专场商品关联
     */
    @ApiOperation("删除专场商品关联")
    @Log(title = "专场商品关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(auctionSessionGoodsService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 从专场中移除指定商品
     */
    @ApiOperation("从专场中移除指定商品")
    @Log(title = "专场商品关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{sessionId}/{goodsId}")
    public AjaxResult removeGoodsFromSession(@PathVariable("sessionId") Long sessionId, @PathVariable("goodsId") Long goodsId)
    {
        LambdaQueryWrapper<AuctionSessionGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AuctionSessionGoods::getSessionId, sessionId)
               .eq(AuctionSessionGoods::getGoodsId, goodsId);
        return toAjax(auctionSessionGoodsService.remove(wrapper));
    }
} 